home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d21 / dvglue.arc / TVTNEW.C < prev    next >
C/C++ Source or Header  |  1990-01-09  |  5KB  |  135 lines

  1. /*================================================*/
  2. /* TVTNEW.C                                       */
  3. /*                                                */
  4. /*  Requires MASM or A86 to recompile             */
  5. /*                                                */
  6. /* (c) Copyright 1988 Ralf Brown                  */
  7. /*     All Rights Reserved                        */
  8. /* May be freely copied for noncommercial use as  */
  9. /* long as this copyright notice is kept intact   */
  10. /* and any changes are indicated in the comment   */
  11. /* blocks for the functions                       */
  12. /*================================================*/
  13.  
  14. #pragma inline
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "tvapi.h"
  19.  
  20. /*================================================*/
  21.  
  22. static int task_busy = 0 ;
  23. static void (*start_addr)(int) ;
  24. static char far *user_stack ;
  25. static int start_row, start_col ;
  26. static int free_stack ;
  27. static int stack_size ;
  28.  
  29. /*================================================*/
  30. /* task_start  initial startup code for task      */
  31. /*   Ralf Brown 5/6/88                            */
  32. /*   Ralf Brown 6/3/88 move initial win pos'ning  */
  33. /*                     into here                  */
  34. /*================================================*/
  35.  
  36. static void far task_start(void)
  37. {
  38.    void (*startaddr)(int) ;
  39.    int parent ;
  40.    char far *stack ;
  41.    int freestack ;
  42.  
  43.    asm mov ax,DGROUP       /* restore DS to proper segment */
  44.    asm mov ds,ax
  45.    asm cli                 /* can't have interrupts when switching stacks */
  46.    asm mov ss,word ptr user_stack+2
  47.    asm mov sp,word ptr user_stack
  48.    asm sti                 /* interrupts OK now */
  49.    asm mov ax,sp           /* store stack pointer for later use */
  50.    asm mov bp,sp           /* set up stack frame */
  51.    asm sub sp,12           /* up to 4 bytes for "startaddr", 8 bytes for others */
  52.    asm mov parent,dx       /* store the segment of parent's task object */
  53.    asm sub ax,word ptr stack_size   /* calculate start of stack area */
  54.    asm mov stack+2,ss
  55.    asm mov stack,ax
  56.  
  57.    startaddr = start_addr ;
  58.    freestack = free_stack ;
  59.    TVwin_move(NIL,start_row,start_col) ;  /* move the window to the desired pos */
  60.  
  61.    /* now we can let the parent task create yet another task, since we're */
  62.    /* done with the static variables */
  63.    task_busy = 0 ;
  64.  
  65.    TVwin_top(NIL) ;        /* make the window active */
  66.    (*startaddr)(parent) ;  /* jump to real start of task */
  67.    TVostack() ;            /* started on private stack, have to switch back */
  68.    if (freestack)
  69.       free((char *)stack) ;
  70.    TVfreetask(NIL) ;       /* task is done, so kill it */
  71. }
  72.  
  73. /*================================================*/
  74. /* TVnew_task  create a new task                  */
  75. /*   Ralf Brown 5/2/88                            */
  76. /*   Ralf Brown 6/3/88 add parms for init win pos */
  77. /*================================================*/
  78.  
  79. OBJECT pascal TVnew_task(OBJECT parent,char *title,int row,int col,int rows,int cols,char *stack,int stacksize,void (*startaddr)(int))
  80. {
  81.    PARMLIST8 p ;
  82.    char old_title[32] ;
  83.    int i ;
  84.  
  85.    while (task_busy)
  86.       ;
  87.    task_busy = 1 ;
  88.    start_addr = startaddr ;
  89.    start_row = row ;
  90.    start_col = col ;
  91.    if (stack == NULL && stacksize == 0)
  92.       return NIL ;  /* can't run without user stack */
  93.    else if (stack == NULL)
  94.       {
  95.       if (stacksize < 256)
  96.          stacksize = 256 ;
  97.       if ((stack = malloc(stacksize)) == NULL)
  98.          return NIL ;  /* can't get stack, so can't run */
  99.       stack = stack + stacksize ;
  100.       free_stack = TRUE ;
  101.       stack_size = stacksize ;
  102.       }
  103.    else
  104.       free_stack = FALSE ;
  105.    user_stack = (char far *) stack ;
  106.    p.num_args = 8 ;
  107.    if (title)
  108.       {
  109.       p.arg[0] = (DWORD)(char far *)title ;
  110.       p.arg[1] = (DWORD) strlen(title) ;
  111.       }
  112.    else
  113.       {
  114.       TVqry_title(parent,old_title,sizeof(old_title)) ;
  115.       p.arg[0] = (DWORD)(char far *)old_title ;
  116.       for (i = sizeof(old_title)-1 ; i > 0 && old_title[i] == ' ' ; i--)
  117.          ;
  118.       p.arg[1] = (DWORD) i ;
  119.       }
  120.    if (rows < 0)
  121.       TVqry_size(parent,&rows,&i) ;
  122.    if (cols < 0)
  123.       TVqry_size(parent,&i,&cols) ;
  124.    p.arg[2] = (DWORD) rows ;
  125.    p.arg[3] = (DWORD) cols ;
  126.    p.arg[4] = (DWORD) 0 ;  /* no additional allocation */
  127.    p.arg[5] = (DWORD) -1 ; /* use default task stack size */
  128.    p.arg[6] = (DWORD) 0 ;
  129.    p.arg[7] = (DWORD) (void far (*)(void)) task_start ;
  130.    TVsendmsg(NEW_MSG, parent?TOS:ME, parent, (PARMLIST *)&p) ;
  131.    return (OBJECT) p.arg[0] ;
  132. }
  133.  
  134. /* End of TVTNEW.C */
  135.